package org.szy.monitor.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.paginate.Page;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.core.handle.ModelAndView;
import org.noear.solon.validation.annotation.NotBlank;
import org.noear.solon.validation.annotation.NotNull;
import org.szy.core.common.controller.BaseController;
import org.szy.core.common.entity.R;
import org.szy.monitor.entity.UserOnline;
import org.szy.monitor.param.UserOnlineQueryParam;
import org.szy.monitor.service.UserOnlineService;
import org.szy.monitor.task.UserOnlineTask;

import java.util.List;

/**
 * 在线用户记录控制器
 *
 * @author wangbuhao
 * @since 2023-06-30 17:41:26
 */
@Controller
@Mapping("/monitor/userOnline")
public class UserOnlineController extends BaseController<UserOnline> {

    private final String PATH_PREFIX = "monitor/userOnline/";
    @Inject
    private UserOnlineService userOnlineService;
    @Inject
    private UserOnlineTask userOnlineTask;

    /**
     * 在线用户记录列表页面
     */
    @Get
    @Mapping("/index")
    @SaCheckPermission(value = "monitor_online", mode = SaMode.OR, orRole = "wangbuhao")
    public ModelAndView index() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "index");
        return view;
    }

    /**
     * 跳转到记录展示页面
     */
    @Mapping(path = "detail", method = MethodType.GET)
    @SaCheckPermission(value = "monitor_online", mode = SaMode.OR, orRole = "wangbuhao")
    public ModelAndView view() {
        return new ModelAndView(PATH_PREFIX + "detail");
    }

    /**
     * 获取在线用户记录分页数据
     *
     * @author wangbuhao
     * @since 2023-06-30 17:41:26
     */
    @Get
    @Mapping("/page")
    public R<Page<UserOnline>> page(UserOnlineQueryParam param) {
        return R.data(userOnlineService.page(getPage(), param));
    }

    @Get
    @Mapping("/getInfo")
    public R<?> getInfo(@NotNull Long id) {
        return R.data(userOnlineService.getById(id));
    }

    /**
     * 删除在线用户记录
     *
     * @author wangbuhao
     * @since 2023-06-28 18:17:40
     */
    @Delete
    @Mapping("/delete")
    public R<?> delete(@NotNull Long id) {
        boolean delete = userOnlineService.removeById(id);
        return R.bool(delete, "删除成功", "删除失败，数据可能正在使用或已删除");
    }

    /**
     * @param tokens 登录标识
     */
    @Mapping(path = "/forceLogout", method = MethodType.DELETE)
    @SaCheckPermission(value = "monitor_online_forceLogout", mode = SaMode.OR, orRole = "wangbuhao")
    public R<?> forceLogout(@NotBlank String tokens) {
        this.log.info("[在线用户]强退：tokens={}", tokens);

        List<String> list = StrUtil.split(tokens, ",");
        for (String item : list) {
            StpUtil.kickoutByTokenValue(item);
            this.userOnlineService.offline(item);
        }

        return R.ok();
    }

    /**
     * 执行离线操作
     */
    @Get
    @Mapping("/doOfflines")
    @SaCheckPermission(value = "monitor_online_doOfflines", mode = SaMode.OR, orRole = "wangbuhao")
    public R<?> doOfflines(){
        userOnlineTask.processOfflines();
        return R.ok();
    }
}
